<?php


namespace app\community\oauth2\Repository;


use app\community\oauth2\Entities\RefreshTokenEntity;
use app\community\oauth2\Model\oauthClientRefreshAccessTokenModel;
use iflow\Response;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;

class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{

    public function getNewRefreshToken()
    {
        // TODO: Implement getNewRefreshToken() method.
        return app(RefreshTokenEntity::class);
    }

    /**
     * 生成新的AccessToken时会执行此函数 此处将新的 RefreshTokenId写入数据库
     * @param RefreshTokenEntityInterface $refreshTokenEntity
     * @return Response
     * @throws \app\community\oauth2\Exception\ClientException
     */
    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity): Response
    {
        // TODO: Implement persistNewRefreshToken() method.
        return (new oauthClientRefreshAccessTokenModel()) -> saveRefreshAccessToken($refreshTokenEntity);
    }

    /**
     * 撤销原有刷新令牌
     * @param string $tokenId
     * @return Response
     */
    public function revokeRefreshToken($tokenId)
    {
        // TODO: Implement revokeRefreshToken() method.
        return (new oauthClientRefreshAccessTokenModel()) -> revokeRefreshAccessToken($tokenId);
    }

    /**
     * 检测token 是否已过期
     * @param string $tokenId
     * @return bool
     * @throws \app\community\oauth2\Exception\ClientNotFoundException
     */
    public function isRefreshTokenRevoked($tokenId)
    {
        // TODO: Implement isRefreshTokenRevoked() method.
        return (new oauthClientRefreshAccessTokenModel()) -> validateRefreshAccessToken($tokenId);
    }
}